package com.ezgo.erc.mq.producer;

import java.text.MessageFormat;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;

/**
 * 
 * (消息生产)模块 TODO(发送消息) Destination 通过指定目的地, 就可以同时指定其发送的消息模式是topic还是queue
 * 
 * @author devintam
 * @date 2016年3月14日 上午11:38:16
 *
 */
@Component
public class MessageSender {

	@Autowired
	private JmsTemplate jmsTemplate;

	@Autowired
	@Qualifier("receiveQueue")
	private Destination receiveDest;

	@Autowired
	@Qualifier("sendQueue")
	private Destination respDest;

	private static final Log logger = LogFactory.getLog(MessageSender.class);

	public void aSyncSendMsg(final Long seq, final Long txnCd, final String context, final Long hNum) {

		jmsTemplate.send(receiveDest, new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				Message msg = session.createTextMessage(context);
				msg.setLongProperty("reqTxnSeq", seq);
				msg.setStringProperty("txnCd", txnCd.toString());
				msg.setLongProperty("hNumber", hNum);
				msg.setBooleanProperty("isaSync", true);
				return msg;
			}
		});
	}

	/**
	 * 发送信息并获取返回信息
	 * 
	 * @param seq
	 * @param txnCd
	 * @param context
	 * @return
	 */
	public Message sendMsg(final Long reqTxnSeq, final Long txnCd, final String context, final Long hNum) {
		jmsTemplate.send(receiveDest, new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				Message msg = session.createTextMessage(context);
				msg.setJMSReplyTo(respDest);
				msg.setLongProperty("reqTxnSeq", reqTxnSeq);
				msg.setStringProperty("txnCd", txnCd.toString());
				msg.setLongProperty("hNumber", hNum);
				msg.setBooleanProperty("isaSync", false);
				return msg;
			}
		});
		System.out.println(Thread.currentThread().getName()
				+ MessageFormat.format("=============成功向{0}发送了一条JMS消息,txnCd_reqTxnSeq_context:{1}", receiveDest.toString(), txnCd + "_" + reqTxnSeq
						+ "_" + context));
		logger.info(Thread.currentThread().getName()
				+ MessageFormat.format("=============成功向{0}发送了一条JMS消息,txnCd_reqTxnSeq_context:{1}", receiveDest.toString(), txnCd + "_" + reqTxnSeq
						+ "_" + context));

		Message msg = jmsTemplate.receiveSelected(respDest, "reqTxnSeq = " + reqTxnSeq);
		if (msg != null) {
			logger.info(Thread.currentThread().getName()
					+ MessageFormat.format("=============成功从{0}收到了一条JMS消息,reqTxnSeq_returnMsg:{1}", receiveDest.toString(), reqTxnSeq + "_" + msg));
			return msg;
		} else {
			logger.error(MessageFormat.format("=============接受消息超时了!reqTxnSeq:{0}", reqTxnSeq));
			return null;
		}
	}

	/**
	 * 发送信息并获取返回文字信息
	 * 
	 * @param seq
	 * @param txnCd
	 * @param context
	 * @return
	 */
	public String sendTextRtnMessage(Long seq, Long txnCd, String context, final Long hNum) {
		TextMessage msg = (TextMessage) sendMsg(seq, txnCd, context, hNum);
		String resp = null;
		try {
			resp = msg.getText();
			logger.info(resp);
		} catch (Exception e) {
			logger.error(MessageFormat.format("get text msg from return message result error : {0}", e.getMessage()));
		}
		return resp;
	}

	public String recivber() {
		TextMessage msg = (TextMessage) jmsTemplate.receive(receiveDest);
		try {
			logger.info(MessageFormat.format("reviced msg is:{0}", msg.getText()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
